/*
 * @lc app=leetcode.cn id=405 lang=csharp
 *
 * [405] 数字转换为十六进制数
 */

// @lc code=start
public class Solution {
    static char[] chars = new char[16] { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
    public string ToHex(int num) {
        if(num == 0) return "0";
        var sb = new StringBuilder();
        for (int i = 7; i >= 0; i --) 
        {
            int val = (num >> (4 * i)) & 0xf;
            if (sb.Length > 0 || val > 0) 
            {
                sb.Append(chars[val]);
            }
        }
        return sb.ToString();
    
    }
}
// @lc code=end

